#define LILYGO_WATCH_2020_V3
#define LILYGO_WATCH_LVGL
#include <LilyGoWatch.h>

TTGOClass *ttgo;

#include "demo_head.h"

static lv_style_t img_style;

lv_obj_t *sw1, *sw2;

BluetoothSerial SerialBT;

/*页面创建状态*/
bool MAIN_STATE = false;
bool CONCTRL_STATE = false;
bool DETECT_STATE = false;
bool HEALTH_STATE = false;
bool WATCH_STATE = false;

bool WATCH_SHOWING = false; //时钟是否正在展示

/*开关状态*/
int SW1_STATE = 0; //灯
int SW2_STATE = 0; //空调
int SW3_STATE = 0; //窗帘

bool EXIT_BT_HAD_CREAT = false; //退出按钮是否创建
lv_obj_t *exit_imgbt_temp;      //用于日历长按退出按钮的删除

/*各项数值的初始化*/
char watch_data[1024];       //接收字符串信息
int temperature = 30;        //当前温度
int humidity = 70;           //当前湿度
int PM2_5 = 40;              //当前PM2.5
int curtains_open = 0;       //窗帘打开程度
short bright_data[24] = {0}; //24小时亮度
/*Set today's date*/
lv_calendar_date_t today = //当前日期
    {
        .year = 2021,
        .month = 8,
        .day = 6,
};

struct Now_time
{
  int hour;
  int min;
  int sec;
} now_time = {
    .hour = hh,
    .min = mm,
    .sec = ss,
};

unsigned int no_touch_time = 0; //未触摸屏幕时间

BMA *sensor;
bool irq = false;
Accel acc;

int stepcount = 0;
int acc_x = 0;
int acc_y = 0;
int acc_z = 0;
int x = 0, y = 0, z = 0;
bool json_is_send = 0;
int require = 0;

/*主创建函数*/
void main_widget_create();
void conctrl_widget_create();
void health_sport_widget_create();
void conctrl_read_and_show();
void light_img_show();
void air_img_show();
void curtains_img_show();
void detect_tileview();
void open_calendar();
lv_obj_t *create_exit_butten(lv_obj_t *call_obj); /*创建退出按钮函数*/

/*定时器相关触发函数*/
void touch_event_handler(lv_obj_t *obj, lv_event_t event);
void detect_tft_is_been_touch_TASK(void *parameter);
void watch_time_count(void);
void acc_data_send(void);

void TaskBlink(void *par);

/*按键反馈回调函数*/
static void switch_light_event_handler(lv_obj_t *obj, lv_event_t event);
static void switch_kong_event_handler(lv_obj_t *obj, lv_event_t event);
static void cur_roller_event_handler(lv_obj_t *obj, lv_event_t event);
static void cur_slider_event_handler(lv_obj_t *obj, lv_event_t event);
static void into_exit_event_handler(lv_obj_t *obj, lv_event_t event);
static void detect_exit_event_handler(lv_obj_t *obj, lv_event_t event);
static void health_exit_event_handler(lv_obj_t *obj, lv_event_t event);
static void conctrler_event_handler(lv_obj_t *obj, lv_event_t event);
static void detect_event_handler(lv_obj_t *obj, lv_event_t event);
static void health_event_handler(lv_obj_t *obj, lv_event_t event);
static void calendar_event_handler(lv_obj_t *obj, lv_event_t event);
static void date_event_handler(lv_obj_t *obj, lv_event_t event);

/*动画函数*/
static void tem_set_angle(void *obj, short int v);
static void arc_set_angle(void *obj, short int v);
static void gauge_set_angle(void *obj, short int v);

/*解析JSON函数*/
void json_to_str(const char *json_str);
/*封装JSON函数*/
void send_json();

/*--------------------------------------主界面点击控制家居后-------------------------------------*/
/*家居控制中，开关1的回调函数*/
static void switch_light_event_handler(lv_obj_t *obj, lv_event_t event)
{
  no_touch_time = 0;
  if (event == LV_EVENT_CLICKED)
  {
    //LV_LOG_USER("State: %s\n", lv_obj_get_state(obj, LV_STATE_CHECKED) ? (SW1_STATE = true) : (SW1_STATE = false));
    if (SW1_STATE)
      SW1_STATE = 0;
    else
      SW1_STATE = 1;

    send_json();
    light_img_show();
  }
}

/*家居控制中，开关2的回调函数*/
static void switch_kong_event_handler(lv_obj_t *obj, lv_event_t event)
{
  no_touch_time = 0;
  if (event == LV_EVENT_CLICKED)
  {
    if (SW2_STATE)
      SW2_STATE = 0;
    else
      SW2_STATE = 1;

    send_json();
    air_img_show();

    //LV_LOG_USER("State: %s\n", lv_obj_get_state(obj, LV_STATE_CHECKED) ? "On" : "Off");
  }
}

/*窗帘滚筒回调函数*/
static void cur_roller_event_handler(lv_obj_t *obj, lv_event_t event)
{
  if (event == LV_EVENT_VALUE_CHANGED)
  {
    char buf[10] = {0};
    lv_roller_get_selected_str(obj, buf, sizeof(buf));
    //printf("Selected month: %s\n", buf);
    if (strcmp(buf, "Open_All") == 0)
    {
      curtains_open = 100;
      SW3_STATE = 1;
      lv_slider_set_value(cur_slider, 100, LV_ANIM_ON);
      //lv_slider_set_anim_time(cur_slider, 300);
    }
    else if (strcmp(buf, "Open_Half") == 0)
    {
      curtains_open = 50;
      SW3_STATE = 1;
      lv_slider_set_value(cur_slider, 50, LV_ANIM_ON);
      //lv_slider_set_anim_time(cur_slider, 300);
    }
    else if (strcmp(buf, "Close_All") == 0)
    {
      curtains_open = 0;
      SW3_STATE = 0;
      lv_slider_set_value(cur_slider, 0, LV_ANIM_ON);
      //lv_slider_set_anim_time(cur_slider, 300);
    }
    send_json();
    curtains_img_show();
  }
}

/*窗帘滑杆回调函数*/
static void cur_slider_event_handler(lv_obj_t *obj, lv_event_t event)
{
  if (event == LV_EVENT_RELEASED)
  {
    curtains_open = lv_slider_get_value(cur_slider);
    if (curtains_open > 0)
      SW3_STATE = 1;
    else
      SW3_STATE = 0;
    send_json();
    curtains_img_show();
  }
}

/*家居控制中，退出按钮的回调函数*/
static void into_exit_event_handler(lv_obj_t *obj, lv_event_t event)
{
  no_touch_time = 0;
  WATCH_SHOWING = false;
  if (event == LV_EVENT_CLICKED)
  {
    main_widget_create();
    lv_scr_load_anim(main_widget, LV_SCR_LOAD_ANIM_OVER_TOP, 500, 0, false);
  }
}

/*家居检测中，退出按钮的回调函数*/
static void detect_exit_event_handler(lv_obj_t *obj, lv_event_t event)
{
  no_touch_time = 0;
  if (event == LV_EVENT_CLICKED)
  {
    //LV_LOG_USER("State: %s\n", lv_obj_get_state(obj, LV_STATE_CHECKED) ? "On" : "Off");
    EXIT_BT_HAD_CREAT = false;
    main_widget_create();
    lv_scr_load_anim(main_widget, LV_SCR_LOAD_ANIM_OVER_BOTTOM, 500, 0, true);
  }
}

/*运动健康中，退出按钮的回调函数*/
static void health_exit_event_handler(lv_obj_t *obj, lv_event_t event)
{
  no_touch_time = 0;
  if (event == LV_EVENT_CLICKED)
  {
    main_widget_create();
    lv_scr_load_anim(main_widget, LV_SCR_LOAD_ANIM_OVER_BOTTOM, 500, 0, false);
  }
}

/*----------------------------------------------主界面点击------------------------------------------------------*/
/* 家居控制触发事件*/
static void conctrler_event_handler(lv_obj_t *obj, lv_event_t event)
{
  no_touch_time = 0;
  if (event == LV_EVENT_CLICKED)
  {
    //Serial.printf("Clicked\n");
    conctrl_widget_create();
    //lv_scr_load_anim(conctrl_widget, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, false);
    lv_scr_load_anim(tabview, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, false);
  }
}

static void detect_event_handler(lv_obj_t *obj, lv_event_t event)
{
  no_touch_time = 0;
  if (event == LV_EVENT_CLICKED)
  {
    Serial.printf("Clicked\n");
    detect_tileview();
    lv_scr_load_anim(tileview, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, false);
  }
  else if (event == LV_EVENT_VALUE_CHANGED)
  {
    Serial.printf("Toggled\n");
  }
}

static void health_event_handler(lv_obj_t *obj, lv_event_t event)
{
  no_touch_time = 0;
  if (event == LV_EVENT_CLICKED)
  {
    Serial.printf("Clicked\n");
    health_sport_widget_create();
    lv_scr_load_anim(sport, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, false);
  }
  else if (event == LV_EVENT_VALUE_CHANGED)
  {
    Serial.printf("Toggled\n");
  }
}

static void calendar_event_handler(lv_obj_t *obj, lv_event_t event)
{
  no_touch_time = 0;
  if (event == LV_EVENT_CLICKED)
  {
    Serial.printf("Clicked\n");
    open_calendar();
    lv_scr_load_anim(calendar_widget, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, false);
  }
  else if (event == LV_EVENT_VALUE_CHANGED)
  {
    Serial.printf("Toggled\n");
  }
}
/*--------------------------------------------------------------------------------------------------------------*/

/*读取控制界面 开关状态 并显示*/
void conctrl_read_and_show()
{
  /*添加开关*/
  sw1 = lv_switch_create(tab1, NULL);
  lv_obj_set_event_cb(sw1, switch_light_event_handler);
  lv_obj_align(sw1, tab1, LV_ALIGN_CENTER, 40, -50);
  if (SW1_STATE)
    lv_switch_on(sw1, LV_ANIM_ON);
  else
    lv_switch_off(sw1, LV_ANIM_ON);

  sw2 = lv_switch_create(tab1, NULL);
  lv_obj_set_event_cb(sw2, switch_kong_event_handler);
  lv_obj_align(sw2, tab1, LV_ALIGN_CENTER, 40, 30);
  if (SW2_STATE)
    lv_switch_on(sw2, LV_ANIM_ON);
  else
    lv_switch_off(sw2, LV_ANIM_ON);
}

/*灯开关图片显示*/
void light_img_show()
{
  /*  添加灯图片与空调图片*/
  if (SW1_STATE)
  {
    LV_IMG_DECLARE(light_on_img);

    img_light_on = lv_img_create(tab1, NULL);
    lv_img_set_src(img_light_on, &light_on_img);
    lv_obj_align(img_light_on, tab1, LV_ALIGN_CENTER, -40, -50);
    if (img_light_off != NULL)
    {
      lv_obj_del(img_light_off);
    }
  }
  else
  {
    LV_IMG_DECLARE(light_off_img);

    img_light_off = lv_img_create(tab1, NULL);
    lv_img_set_src(img_light_off, &light_off_img);
    lv_obj_align(img_light_off, tab1, LV_ALIGN_CENTER, -40, -50);
    if (img_light_on != NULL)
    {
      lv_obj_del(img_light_on);
    }
  }
}

/*空调开关图片显示*/
void air_img_show()
{
  if (SW2_STATE)
  {
    LV_IMG_DECLARE(air_on_img);
    /*创建背景图*/
    img_air_on = lv_img_create(tab1, NULL);
    lv_img_set_src(img_air_on, &air_on_img);
    lv_obj_align(img_air_on, tab1, LV_ALIGN_CENTER, -40, 30);
    if (img_air_off != NULL)
    {
      lv_obj_del(img_air_off);
    }
  }
  else
  {
    LV_IMG_DECLARE(air_off_img);
    /*创建背景图*/
    img_air_off = lv_img_create(tab1, NULL);
    lv_img_set_src(img_air_off, &air_off_img);
    lv_obj_align(img_air_off, tab1, LV_ALIGN_CENTER, -40, 30);
    if (img_air_on != NULL)
    {
      lv_obj_del(img_air_on);
    }
  }
}

/*窗帘开关图片显示*/
void curtains_img_show()
{
  /*  添加灯图片与空调图片*/
  if (SW3_STATE == 1 && img_cur_on == NULL)
  {
    LV_IMG_DECLARE(cur_open);

    img_cur_on = lv_img_create(tab2, NULL);
    lv_img_set_src(img_cur_on, &cur_open);
    lv_obj_align(img_cur_on, tab2, LV_ALIGN_CENTER, -60, -30);
    if (img_cur_off != NULL)
    {
      lv_obj_del(img_cur_off);
      img_cur_off = NULL;
    }
  }
  else if (SW3_STATE == 0 && img_cur_off == NULL)
  {
    LV_IMG_DECLARE(cur_close);

    img_cur_off = lv_img_create(tab2, NULL);
    lv_img_set_src(img_cur_off, &cur_close);
    lv_obj_align(img_cur_off, tab2, LV_ALIGN_CENTER, -60, -30);
    if (img_cur_on != NULL)
    {
      lv_obj_del(img_cur_on);
      img_cur_on = NULL;
    }
  }
}

/*创建退出按钮函数*/
lv_obj_t *create_exit_butten(lv_obj_t *call_obj)
{
  /*添加退出按钮*/
  LV_IMG_DECLARE(exit_t);
  LV_IMG_DECLARE(exit_ts);
  lv_obj_t *eixt_imgbt = lv_imgbtn_create(call_obj, NULL);
  lv_style_set_image_recolor_opa(&img_style, LV_STATE_PRESSED, LV_OPA_30);
  lv_style_set_image_recolor(&img_style, LV_STATE_PRESSED, LV_COLOR_BLACK);
  lv_style_set_text_color(&img_style, LV_STATE_DEFAULT, LV_COLOR_WHITE);

  lv_imgbtn_set_src(eixt_imgbt, LV_BTN_STATE_RELEASED, &exit_t);
  lv_imgbtn_set_src(eixt_imgbt, LV_BTN_STATE_PRESSED, &exit_ts);
  lv_imgbtn_set_checkable(eixt_imgbt, true);
  lv_obj_add_style(eixt_imgbt, LV_IMGBTN_PART_MAIN, &img_style);
  lv_obj_align(eixt_imgbt, call_obj, LV_ALIGN_CENTER, 0, 80);

  lv_obj_set_event_cb(eixt_imgbt, detect_exit_event_handler);

  return eixt_imgbt;
}

/*--------------------温度、湿度、PM2.5动画值设置函数---------------------------*/
static void tem_set_angle(void *obj, short int v)
{
  lv_linemeter_set_value((lv_obj_t *)obj, v);
}

static void arc_set_angle(void *obj, short int v)
{
  lv_arc_set_value((lv_obj_t *)obj, v);
}

static void gauge_set_angle(void *obj, short int v)
{
  lv_gauge_set_value((lv_obj_t *)obj, 0, v);
}
/*-----------------------------------------------------------------------------*/

/*点击日历反馈函数*/
static void date_event_handler(lv_obj_t *obj, lv_event_t event)
{
  no_touch_time = 0;
  if (event == LV_EVENT_CLICKED)
  {
    lv_calendar_date_t *date = lv_calendar_get_pressed_date(obj);
    Serial.printf("STATE: %d", touch_pad_get_status());
    if (date)
    {
      Serial.printf("Clicked date: %02d.%02d.%d\n", date->day, date->month, date->year);
    }
  }
  else if (event == LV_EVENT_LONG_PRESSED && EXIT_BT_HAD_CREAT == false)
  {
    EXIT_BT_HAD_CREAT = true;
    Serial.printf("LONG PRESSED");
    exit_imgbt_temp = create_exit_butten(obj);
  }
  else if (event == LV_EVENT_LONG_PRESSED && EXIT_BT_HAD_CREAT == true)
  {
    EXIT_BT_HAD_CREAT = false;
    lv_obj_del(exit_imgbt_temp);
  }
}

/*创建打开日历*/
void open_calendar()
{
  /*  设置背景图片*/
  LV_IMG_DECLARE(background_new);

  calendar_widget = lv_obj_create(NULL, NULL);
  lv_obj_set_size(calendar_widget, 240, 240);
  lv_obj_t *cal_img = lv_img_create(calendar_widget, NULL);
  lv_img_set_src(cal_img, &background_new);
  lv_obj_align(cal_img, calendar_widget, LV_ALIGN_CENTER, 0, 0);
  lv_obj_set_size(cal_img, 240, 240);

  lv_obj_t *calendar = lv_calendar_create(calendar_widget, NULL);
  lv_obj_set_size(calendar, 235, 235);
  lv_obj_align(calendar, NULL, LV_ALIGN_CENTER, 0, 0);
  lv_obj_set_event_cb(calendar, date_event_handler);

  /*Make the date number smaller to be sure they fit into their area*/
  lv_obj_set_style_local_text_font(calendar, LV_CALENDAR_PART_DATE, LV_STATE_DEFAULT, lv_theme_get_font_small());

  lv_calendar_set_today_date(calendar, &today);
  lv_calendar_set_showed_date(calendar, &today);

  /*Highlight a few days*/
  static lv_calendar_date_t highlighted_days[3]; /*Only its pointer will be saved so should be static*/
  highlighted_days[0].year = today.year;
  highlighted_days[0].month = today.month;
  highlighted_days[0].day = today.day;

  highlighted_days[1].year = 2018;
  highlighted_days[1].month = 10;
  highlighted_days[1].day = 11;

  highlighted_days[2].year = 2018;
  highlighted_days[2].month = 11;
  highlighted_days[2].day = 22;

  lv_calendar_set_highlighted_dates(calendar, highlighted_days, 3);
}

/*家居检测页面创建*/
void detect_tileview()
{
  LV_IMG_DECLARE(background_new);
  static lv_point_t valid_pos[] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};

  tileview = lv_tileview_create(NULL, NULL);
  lv_tileview_set_valid_positions(tileview, valid_pos, 4);
  lv_tileview_set_edge_flash(tileview, true);

  lv_obj_set_event_cb(tileview, touch_event_handler);
  //lv_tileview_set_tile_act(tileview, 0 , 0, LV_ANIM_ON);

  /*---------------------------------温度检测-----------------------------------------*/
  /*      创建tile*/
  lv_obj_t *tile1 = lv_obj_create(tileview, NULL);
  lv_obj_set_size(tile1, LV_HOR_RES, LV_VER_RES);

  lv_tileview_add_element(tileview, tile1);

  lv_obj_t *img_indetect1 = lv_img_create(tile1, NULL);
  lv_img_set_src(img_indetect1, &background_new);
  lv_obj_align(img_indetect1, NULL, LV_ALIGN_CENTER, 0, 0);
  lv_obj_set_size(img_indetect1, 240, 240);

  //-创建温度仪表--//
  lv_obj_t *temp_meter = lv_linemeter_create(tile1, NULL);

  lv_linemeter_set_range(temp_meter, -60, 60);
  lv_linemeter_set_value(temp_meter, temperature); /*Set the current value*/
  lv_linemeter_set_scale(temp_meter, 240, 100);    /*Set the angle and number of lines*/
  lv_obj_set_size(temp_meter, 150, 150);
  lv_obj_align(temp_meter, tile1, LV_ALIGN_CENTER, 0, 0);

  /*      温度动画*/
  lv_anim_t tem_a;
  lv_anim_init(&tem_a);
  lv_anim_set_var(&tem_a, temp_meter);
  lv_anim_set_exec_cb(&tem_a, tem_set_angle);
  lv_anim_set_time(&tem_a, 1000);
  lv_anim_set_repeat_count(&tem_a, 10000); /*Just for the demo*/
  lv_anim_set_repeat_delay(&tem_a, 1000);
  lv_anim_set_values(&tem_a, -60, temperature);
  lv_anim_start(&tem_a);

  char tem_str[40], tem_sim[30] = "\t#0000ff °C#\n\n-60\t\t  \t\t60";
  sprintf(tem_str, "%d%s", temperature, tem_sim);

  lv_obj_t *tem_label = lv_label_create(temp_meter, NULL);
  lv_label_set_recolor(tem_label, true);
  lv_label_set_align(tem_label, LV_LABEL_ALIGN_CENTER);
  lv_label_set_text(tem_label, tem_str);
  lv_obj_align(tem_label, temp_meter, LV_ALIGN_CENTER, 0, 20);

  /*Tile1: just a label*/
  lv_obj_t *tem_title_label = lv_label_create(tile1, NULL);
  lv_label_set_text(tem_title_label, "Temperature");
  lv_obj_align(tem_title_label, NULL, LV_ALIGN_CENTER, 0, -100);

  lv_obj_t *scroll_lab = lv_label_create(tile1, NULL);
  lv_label_set_long_mode(scroll_lab, LV_LABEL_LONG_SROLL_CIRC);
  lv_obj_set_width(scroll_lab, 150);
  lv_obj_align(scroll_lab, NULL, LV_ALIGN_CENTER, 0, 100);
  if (temperature >= 30)
    lv_label_set_text(scroll_lab, "It's very hot. Be careful not to get heatstroke !");
  else if (temperature > 15 || temperature < 30)
    lv_label_set_text(scroll_lab, "The temperature is comfortable and suitable for travel !");
  else if (temperature <= 15)
    lv_label_set_text(scroll_lab, "It's very cold. Keep warm !");

  /*-------------------------------------湿度检测------------------------------------------------*/

  lv_obj_t *img_indetect2 = lv_img_create(tileview, NULL);
  lv_img_set_src(img_indetect2, &background_new);
  lv_obj_align(img_indetect2, NULL, LV_ALIGN_CENTER, 0, 0);
  lv_obj_set_size(img_indetect2, 240, 240);
  lv_obj_set_pos(img_indetect2, 0, LV_VER_RES);

  /*Tile2: 一个湿度弧*/
  lv_obj_t *arc = lv_arc_create(img_indetect2, NULL);
  lv_arc_set_end_angle(arc, 240);
  lv_arc_set_range(arc, 0, 100);
  lv_arc_set_value(arc, 70);
  lv_obj_set_size(arc, 150, 150);
  lv_obj_align(arc, NULL, LV_ALIGN_CENTER, 0, 0);

  /*      湿度圆弧动画*/
  lv_anim_t arc_a;
  lv_anim_init(&arc_a);
  lv_anim_set_var(&arc_a, arc);
  lv_anim_set_exec_cb(&arc_a, arc_set_angle);
  lv_anim_set_time(&arc_a, 1000);
  lv_anim_set_repeat_count(&arc_a, 10000); /*Just for the demo*/
  lv_anim_set_repeat_delay(&arc_a, 1000);
  lv_anim_set_values(&arc_a, 0, humidity);
  lv_anim_start(&arc_a);

  /*湿度弧相关lable*/
  char hum_str[40], hum_sim[30] = "\t#0000ff %#\n\n\n0\t    \t100";
  sprintf(hum_str, "%d%s", humidity, hum_sim);

  lv_obj_t *hum_label = lv_label_create(arc, NULL);
  lv_label_set_recolor(hum_label, true);
  lv_label_set_align(hum_label, LV_LABEL_ALIGN_CENTER);
  lv_label_set_text(hum_label, hum_str);
  lv_obj_align(hum_label, arc, LV_ALIGN_CENTER, 0, 20);

  /*湿度label*/
  lv_obj_t *hum_title_label = lv_label_create(img_indetect2, NULL);
  lv_label_set_text(hum_title_label, "Humidity");
  lv_obj_align(hum_title_label, NULL, LV_ALIGN_CENTER, 0, -100);

  lv_obj_t *hum_scroll_lab = lv_label_create(img_indetect2, NULL);
  lv_label_set_long_mode(hum_scroll_lab, LV_LABEL_LONG_SROLL_CIRC);
  lv_obj_set_width(hum_scroll_lab, 150);
  lv_obj_align(hum_scroll_lab, NULL, LV_ALIGN_CENTER, 0, 100);
  if (humidity >= 80)
    lv_label_set_text(hum_scroll_lab, "The air is humid, pay attention to food hygiene !");
  else if (humidity > 40 || humidity < 80)
    lv_label_set_text(hum_scroll_lab, "The air is comfortable. Let's go for a walk !");
  else if (humidity <= 40)
    lv_label_set_text(hum_scroll_lab, "The air is dry, beware of fire !");

  /*-------------------------------PM2.5----------------------------------*/
  //创建窗口
  lv_obj_t *tile3 = lv_obj_create(tileview, tile1);
  lv_obj_set_pos(tile3, LV_HOR_RES, LV_VER_RES);
  lv_tileview_add_element(tileview, tile3);

  lv_obj_t *img_indetect3 = lv_img_create(tile3, NULL);
  lv_img_set_src(img_indetect3, &background_new);
  lv_obj_align(img_indetect3, NULL, LV_ALIGN_CENTER, 0, 0);
  lv_obj_set_size(img_indetect3, 240, 240);

  /*Describe the color for the needles*/
  static lv_color_t needle_colors[3];
  needle_colors[0] = LV_COLOR_BLUE;
  needle_colors[1] = LV_COLOR_RED;

  /*Create a gauge*/
  lv_obj_t *gauge1 = lv_gauge_create(tile3, NULL);
  lv_gauge_set_needle_count(gauge1, 2, needle_colors);
  lv_obj_set_size(gauge1, 180, 180);
  lv_gauge_set_critical_value(gauge1, 75);
  lv_obj_align(gauge1, NULL, LV_ALIGN_CENTER, 0, 0);

  /*Set the values*/
  lv_gauge_set_value(gauge1, 0, PM2_5);
  lv_gauge_set_value(gauge1, 1, 75);

  /* PM2.5指针动画动画*/
  lv_anim_t gauge_a;
  lv_anim_init(&gauge_a);
  lv_anim_set_var(&gauge_a, gauge1);
  lv_anim_set_exec_cb(&gauge_a, gauge_set_angle);
  lv_anim_set_time(&gauge_a, 1000);
  lv_anim_set_repeat_count(&gauge_a, 10000); /*Just for the demo*/
  lv_anim_set_repeat_delay(&gauge_a, 1500);
  lv_anim_set_values(&gauge_a, 0, PM2_5);
  lv_anim_start(&gauge_a);

  char pm_str[40], pm_col[10] = "#00ff00 ", pm_sim[30] = "#\n\n#0000ff ug/m3#";
  sprintf(pm_str, "%s%d%s", pm_col, PM2_5, pm_sim);

  /*PM2.5 label*/
  lv_obj_t *pm_title_label = lv_label_create(tile3, NULL);
  lv_label_set_text(pm_title_label, "PM2.5");
  lv_obj_align(pm_title_label, NULL, LV_ALIGN_CENTER, 0, -100);

  /*PM2.5 单位label*/
  lv_obj_t *pm_label = lv_label_create(tile3, NULL);
  lv_label_set_recolor(pm_label, true);
  lv_label_set_align(pm_label, LV_LABEL_ALIGN_CENTER);
  lv_label_set_text(pm_label, pm_str);
  lv_obj_align(pm_label, NULL, LV_ALIGN_CENTER, 0, 60);

  /*创建LED*/
  static lv_style_t led1_style;
  lv_style_init(&led1_style);
  lv_style_set_bg_color(&led1_style, LV_STATE_DEFAULT, LV_COLOR_BLUE);
  lv_obj_t *led1 = lv_led_create(tile3, NULL);
  lv_obj_add_style(led1, LV_LED_PART_MAIN, &led1_style);
  lv_led_set_bright(led1, 150);
  lv_obj_align(led1, tile3, LV_ALIGN_CENTER, -85, 100);
  lv_obj_set_size(led1, 25, 25);

  static lv_style_t led2_style;
  lv_style_init(&led2_style);
  lv_style_set_bg_color(&led2_style, LV_STATE_DEFAULT, LV_COLOR_RED);
  lv_obj_t *led2 = lv_led_create(tile3, NULL);
  lv_obj_add_style(led2, LV_LED_PART_MAIN, &led2_style);
  lv_led_set_bright(led2, 150);
  lv_obj_align(led2, tile3, LV_ALIGN_CENTER, -85, -85);
  lv_obj_set_size(led2, 25, 25);

  /*PM2.5 label*/
  lv_obj_t *dan_label = lv_label_create(tile3, NULL);
  lv_label_set_recolor(dan_label, true);
  lv_label_set_text(dan_label, "#ff0000 Danger#");
  lv_obj_align(dan_label, NULL, LV_ALIGN_CENTER, -85, -75);

  lv_obj_t *saf_label = lv_label_create(tile3, NULL);
  lv_label_set_recolor(saf_label, true);
  lv_label_set_text(saf_label, "#0000ff Safety#");
  lv_obj_align(saf_label, NULL, LV_ALIGN_CENTER, -88, 70);

  if (PM2_5 < 75)
  {
    lv_led_on(led1);
    lv_led_off(led2);
  }
  else
  {
    lv_led_on(led2);
    lv_led_off(led1);
  }

  /*------------------------------亮度检测----------------------------------------*/
  lv_obj_t *img_indetect4 = lv_img_create(tileview, NULL);
  lv_img_set_src(img_indetect4, &background_new);
  lv_obj_align(img_indetect4, NULL, LV_ALIGN_CENTER, 120, -120);
  lv_obj_set_size(img_indetect4, 240, 240);

  /*创建亮度图表*/
  lv_obj_t *bright_chart = lv_chart_create(img_indetect4, NULL);
  lv_chart_set_range(bright_chart, 0, 1000);
  lv_chart_set_point_count(bright_chart, 24);
  lv_obj_set_size(bright_chart, 200, 150);
  lv_obj_align(bright_chart, NULL, LV_ALIGN_CENTER, 0, -30);
  lv_chart_set_type(bright_chart, LV_CHART_TYPE_LINE);

  /*Add a data series*/
  lv_chart_series_t *ser1 = lv_chart_add_series(bright_chart, LV_COLOR_GREEN);
  lv_chart_set_points(bright_chart, ser1, bright_data);

  /*添加退出按钮*/
  LV_IMG_DECLARE(exit_t);
  LV_IMG_DECLARE(exit_ts);
  lv_obj_t *eixt_imgbt = lv_imgbtn_create(img_indetect4, NULL);
  lv_style_set_image_recolor_opa(&img_style, LV_STATE_PRESSED, LV_OPA_30);
  lv_style_set_image_recolor(&img_style, LV_STATE_PRESSED, LV_COLOR_BLACK);
  lv_style_set_text_color(&img_style, LV_STATE_DEFAULT, LV_COLOR_WHITE);

  lv_imgbtn_set_src(eixt_imgbt, LV_BTN_STATE_RELEASED, &exit_t);
  lv_imgbtn_set_src(eixt_imgbt, LV_BTN_STATE_PRESSED, &exit_ts);
  lv_imgbtn_set_checkable(eixt_imgbt, true);
  lv_obj_add_style(eixt_imgbt, LV_IMGBTN_PART_MAIN, &img_style);
  lv_obj_align(eixt_imgbt, img_indetect4, LV_ALIGN_CENTER, 75, 75);

  lv_obj_set_event_cb(eixt_imgbt, detect_exit_event_handler);

  /*创建亮度单位相关lable*/
  lv_obj_t *bri_label1 = lv_label_create(img_indetect4, NULL);
  lv_label_set_recolor(bri_label1, true);
  lv_label_set_text(bri_label1, "#0000ff 1000/lx#");
  lv_obj_align(bri_label1, bright_chart, LV_ALIGN_CENTER, -95, -83);

  lv_obj_t *bri_label2 = lv_label_create(img_indetect4, NULL);
  lv_label_set_recolor(bri_label2, true);
  lv_label_set_text(bri_label2, "#0000ff 0/lx#");
  lv_obj_align(bri_label2, bright_chart, LV_ALIGN_CENTER, -95, 85);

  lv_obj_t *bri_scroll_lab = lv_label_create(img_indetect4, NULL);
  lv_label_set_long_mode(bri_scroll_lab, LV_LABEL_LONG_SROLL_CIRC);
  lv_obj_set_width(bri_scroll_lab, 90);
  lv_obj_align(bri_scroll_lab, bright_chart, LV_ALIGN_CENTER, 0, 90);
  lv_label_set_text(bri_scroll_lab, "The brightness of the day");
}

/*创建控制界面*/
void conctrl_widget_create()
{
  if (!CONCTRL_STATE)
  {
    CONCTRL_STATE = true;

    tabview = lv_tabview_create(NULL, NULL);

    /*Add 3 tabs (the tabs are page (lv_page) and can be scrolled*/
    tab1 = lv_tabview_add_tab(tabview, "Light&Air");
    tab2 = lv_tabview_add_tab(tabview, "Curtains");
    tab3 = lv_tabview_add_tab(tabview, "Exit&Mes");

    lv_obj_set_event_cb(tabview, touch_event_handler);

    LV_IMG_DECLARE(conctrl_back);
    /*设置控制界面背景*/
    lv_obj_t *img_incon = lv_img_create(tab1, NULL);
    lv_img_set_src(img_incon, &conctrl_back);
    lv_obj_align(img_incon, tab1, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_size(img_incon, 240, 175);

    conctrl_read_and_show();
    light_img_show();
    air_img_show();

    /*-----------------窗帘控制页面----------------*/
    /*设置控制界面背景*/
    img_incon = lv_img_create(tab2, NULL);
    lv_img_set_src(img_incon, &conctrl_back);
    lv_obj_align(img_incon, tab2, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_size(img_incon, 220, 175);

    /*创建滚筒*/
    cur_roller = lv_roller_create(tab2, NULL);
    lv_roller_set_options(cur_roller,
                          "Open_All\n"
                          "Open_Half\n"
                          "Close_All",
                          LV_ROLLER_MODE_INIFINITE);
    lv_obj_set_width(cur_roller, 80);
    lv_roller_set_visible_row_count(cur_roller, 3);
    lv_obj_align(cur_roller, tab2, LV_ALIGN_CENTER, 60, -30);
    lv_obj_set_event_cb(cur_roller, cur_roller_event_handler);

    /*Create a slider*/
    cur_slider = lv_slider_create(tab2, NULL);
    lv_obj_align(cur_slider, tab2, LV_ALIGN_CENTER, 50, 50);
    lv_obj_set_width(cur_slider, 180);
    lv_slider_set_range(cur_slider, 0, 100);
    lv_slider_set_anim_time(cur_slider, 300);
    lv_obj_set_event_cb(cur_slider, cur_slider_event_handler);

    curtains_img_show();

    /*设置控制界面背景*/
    img_incon = lv_img_create(tab3, NULL);
    lv_img_set_src(img_incon, &conctrl_back);
    lv_obj_align(img_incon, tab3, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_size(img_incon, 220, 180);

    /*添加退出图片按钮*/
    LV_IMG_DECLARE(exit_t);
    LV_IMG_DECLARE(exit_ts);
    lv_obj_t *imgbtn_cont_exit = lv_imgbtn_create(tab3, NULL);

    lv_style_set_image_recolor_opa(&img_style, LV_STATE_PRESSED, LV_OPA_30);
    lv_style_set_image_recolor(&img_style, LV_STATE_PRESSED, LV_COLOR_BLACK);
    lv_style_set_text_color(&img_style, LV_STATE_DEFAULT, LV_COLOR_WHITE);

    lv_imgbtn_set_src(imgbtn_cont_exit, LV_BTN_STATE_RELEASED, &exit_t);
    lv_imgbtn_set_src(imgbtn_cont_exit, LV_BTN_STATE_PRESSED, &exit_ts);
    lv_imgbtn_set_checkable(imgbtn_cont_exit, true);
    lv_obj_add_style(imgbtn_cont_exit, LV_IMGBTN_PART_MAIN, &img_style);
    lv_obj_align(imgbtn_cont_exit, tab3, LV_ALIGN_CENTER, 0, 55);

    lv_obj_set_event_cb(imgbtn_cont_exit, into_exit_event_handler);

    /*Create a page*/
    lv_obj_t *user_page = lv_page_create(tab3, NULL);
    lv_obj_set_size(user_page, 200, 110);
    lv_obj_align(user_page, tab3, LV_ALIGN_CENTER, 0, -30);

    /*Create a label on the page*/
    user_label = lv_label_create(user_page, NULL);
    lv_label_set_long_mode(user_label, LV_LABEL_LONG_BREAK);        /*Automatically break long lines*/
    lv_obj_set_width(user_label, lv_page_get_width_fit(user_page)); /*Set the label width to max value to not show hor. scroll bars*/
    lv_label_set_text(user_label, watch_data);
  }
  else
  {
    return;
  }
}

/*创建健康运动界面*/
void health_sport_widget_create()
{
  if (HEALTH_STATE == false)
  {
    HEALTH_STATE = true;
    sport = lv_obj_create(NULL, NULL);

    LV_IMG_DECLARE(background_new);
    lv_obj_t *img_sport = lv_img_create(sport, NULL);
    lv_img_set_src(img_sport, &background_new);
    lv_obj_align(img_sport, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_size(img_sport, 240, 240);

    LV_IMG_DECLARE(acc_con);
    lv_obj_t *img_acc = lv_img_create(sport, NULL);
    lv_img_set_src(img_acc, &acc_con);
    lv_obj_align(img_acc, NULL, LV_ALIGN_CENTER, -60, -70);

    LV_IMG_DECLARE(step);
    lv_obj_t *img_step = lv_img_create(sport, NULL);
    lv_img_set_src(img_step, &step);
    lv_obj_align(img_step, NULL, LV_ALIGN_CENTER, -60, 20);

    label_acc = lv_label_create(sport, NULL);
    lv_label_set_recolor(label_acc, true);
    lv_label_set_align(label_acc, LV_LABEL_ALIGN_CENTER);
    lv_obj_align(label_acc, sport, LV_ALIGN_CENTER, 10, -70);
    lv_obj_set_size(label_acc, 80, 30);

    label_step = lv_label_create(sport, NULL);
    lv_label_set_recolor(label_step, true);
    lv_label_set_align(label_step, LV_LABEL_ALIGN_CENTER);
    lv_obj_align(label_step, sport, LV_ALIGN_CENTER, 60, 20);

    /*添加退出按钮*/
    LV_IMG_DECLARE(exit_t);
    LV_IMG_DECLARE(exit_ts);
    lv_obj_t *eixt_imgbt = lv_imgbtn_create(sport, NULL);
    lv_style_set_image_recolor_opa(&img_style, LV_STATE_PRESSED, LV_OPA_30);
    lv_style_set_image_recolor(&img_style, LV_STATE_PRESSED, LV_COLOR_BLACK);
    lv_style_set_text_color(&img_style, LV_STATE_DEFAULT, LV_COLOR_WHITE);

    lv_imgbtn_set_src(eixt_imgbt, LV_BTN_STATE_RELEASED, &exit_t);
    lv_imgbtn_set_src(eixt_imgbt, LV_BTN_STATE_PRESSED, &exit_ts);
    lv_imgbtn_set_checkable(eixt_imgbt, true);
    lv_obj_add_style(eixt_imgbt, LV_IMGBTN_PART_MAIN, &img_style);
    lv_obj_align(eixt_imgbt, sport, LV_ALIGN_CENTER, 75, 75);

    lv_obj_set_event_cb(eixt_imgbt, health_exit_event_handler);
  }
}

/*创建主界面*/
void main_widget_create()
{
  if (!MAIN_STATE)
  {
    MAIN_STATE = true;
    /*  设置背景图片*/
    LV_IMG_DECLARE(background_new);

    main_widget = lv_obj_create(NULL, NULL);
    lv_obj_set_size(main_widget, 240, 240);
    lv_obj_t *img1 = lv_img_create(main_widget, NULL);
    lv_img_set_src(img1, &background_new);
    lv_obj_align(img1, main_widget, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_size(img1, 240, 240);

    /*  添加图片按钮*/
    LV_IMG_DECLARE(conctrler);
    LV_IMG_DECLARE(detect);
    LV_IMG_DECLARE(health);
    LV_IMG_DECLARE(date);

    /*   设置图片按钮样式*/

    lv_style_init(&img_style);
    lv_style_set_image_recolor_opa(&img_style, LV_STATE_PRESSED, LV_OPA_30);
    lv_style_set_image_recolor(&img_style, LV_STATE_PRESSED, LV_COLOR_BLACK);
    lv_style_set_text_color(&img_style, LV_STATE_DEFAULT, LV_COLOR_WHITE);

    /*Create an Image button*/
    lv_obj_t *imgbtn1 = lv_imgbtn_create(main_widget, NULL);
    lv_imgbtn_set_src(imgbtn1, LV_BTN_STATE_RELEASED, &conctrler);
    lv_imgbtn_set_src(imgbtn1, LV_BTN_STATE_PRESSED, &conctrler);
    lv_imgbtn_set_checkable(imgbtn1, true);
    lv_obj_add_style(imgbtn1, LV_IMGBTN_PART_MAIN, &img_style);
    lv_obj_align(imgbtn1, main_widget, LV_ALIGN_CENTER, -53, -53);

    /*Create an Image button*/
    lv_obj_t *imgbtn2 = lv_imgbtn_create(main_widget, NULL);
    lv_imgbtn_set_src(imgbtn2, LV_BTN_STATE_RELEASED, &detect);
    lv_imgbtn_set_src(imgbtn2, LV_BTN_STATE_PRESSED, &detect);
    lv_imgbtn_set_checkable(imgbtn2, true);
    lv_obj_add_style(imgbtn2, LV_IMGBTN_PART_MAIN, &img_style);
    lv_obj_align(imgbtn2, main_widget, LV_ALIGN_CENTER, -53, 53);

    /*Create an Image button*/
    lv_obj_t *imgbtn3 = lv_imgbtn_create(main_widget, NULL);
    lv_imgbtn_set_src(imgbtn3, LV_BTN_STATE_RELEASED, &health);
    lv_imgbtn_set_src(imgbtn3, LV_BTN_STATE_PRESSED, &health);
    lv_imgbtn_set_checkable(imgbtn3, true);
    lv_obj_add_style(imgbtn3, LV_IMGBTN_PART_MAIN, &img_style);
    lv_obj_align(imgbtn3, main_widget, LV_ALIGN_CENTER, 53, -53);

    /*Create an Image button*/
    lv_obj_t *imgbtn4 = lv_imgbtn_create(main_widget, NULL);
    lv_imgbtn_set_src(imgbtn4, LV_BTN_STATE_RELEASED, &date);
    lv_imgbtn_set_src(imgbtn4, LV_BTN_STATE_PRESSED, &date);
    lv_imgbtn_set_checkable(imgbtn4, true);
    lv_obj_add_style(imgbtn4, LV_IMGBTN_PART_MAIN, &img_style);
    lv_obj_align(imgbtn4, main_widget, LV_ALIGN_CENTER, 53, 53);

    /*  添加按钮事件*/
    lv_obj_set_event_cb(imgbtn1, conctrler_event_handler);
    lv_obj_set_event_cb(imgbtn2, detect_event_handler);
    lv_obj_set_event_cb(imgbtn3, health_event_handler);
    lv_obj_set_event_cb(imgbtn4, calendar_event_handler);

    lv_obj_set_event_cb(main_widget, touch_event_handler);
  }
  else
    return;
}

/*屏幕触摸计时器重新置零*/
void touch_event_handler(lv_obj_t *obj, lv_event_t event)
{
  if (event == LV_EVENT_PRESSED)
  {
    no_touch_time = 0;
  }
}

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  ttgo = TTGOClass::getWatch();
  ttgo->begin();
  ttgo->openBL();
  ttgo->lvgl_begin();

  /*-------------------加速度传感器------------------*/
  sensor = ttgo->bma;
  // Accel parameter structure
  Acfg cfg;
  cfg.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
  cfg.range = BMA4_ACCEL_RANGE_2G;
  cfg.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
  cfg.perf_mode = BMA4_CONTINUOUS_MODE;
  sensor->accelConfig(cfg);
  sensor->enableAccel();

  pinMode(BMA423_INT1, INPUT);
  attachInterrupt(
      BMA423_INT1, []
      {
        // Set interrupt to set irq value to 1
        irq = 1;
      },
      RISING); //It must be a rising edge

  // Enable BMA423 step count feature
  sensor->enableFeature(BMA423_STEP_CNTR, true);
  // Reset steps
  sensor->resetStepCounter();
  // Turn on step interrupt
  sensor->enableStepCountInterrupt();
  /*--------------------------------------------------*/

  /*定时器初始化*/
  hw_timer_t *timer = timerBegin(0, 80, true);
  timerAttachInterrupt(timer, watch_time_count, true);
  timerAlarmWrite(timer, 2500000, true);
  timerAlarmEnable(timer);

  SerialBT.begin("Smart-Watch");
  SerialBT.enableSSP();

  if (SerialBT.isReady())
    Serial.printf("OK");

  /*任务创建*/
  // xTaskCreate(
  //     detect_tft_is_been_touch_TASK,
  //     "detect_tft_is_been_touch_TASK",
  //     1000,
  //     NULL,
  //     2,
  //     NULL);

  analog(); //开机先进入时钟态
  lv_scr_load_anim(central, LV_SCR_LOAD_ANIM_FADE_ON, 200, 0, true);
}

void loop()
{
  //put your main code here, to run repeatedly:
  lv_task_handler();
  delay(5);

  if (SerialBT.available())
  {
    /*调用函数解析接收到的数据*/
    const char *data = SerialBT.readString().c_str();
    Serial.printf(data);

    char databuf[1024] = {0};
    strcpy(databuf, data);

    const char end_str = '\0';
    strcat(databuf, &end_str);
    Serial.printf(databuf);
    json_to_str(databuf);
    data = NULL;
  }

  if (irq)
  {
    irq = 0;
    bool rlst;
    rlst = sensor->readInterrupt();
    if (rlst && sensor->isStepCounter())
    {
      stepcount = sensor->getCounter();
      Serial.printf("Stepcount: %d\n", stepcount);
    }
  }

  if (json_is_send == false)
  {
    bool acc_res = sensor->getAccel(acc);
    if (acc_res == false)
      Serial.printf("GetAccel Fail");
    else
    {
      x = acc.x - acc_x;
      acc_x = acc.x;
      y = acc.y - acc_y;
      acc_y = acc.y;
      z = acc.z - acc_z;
      acc_z = acc.z;
      if (labs(x) > 10 || labs(y) > 10 || labs(z) > 10)
      {
        if (HEALTH_STATE)
        {
          lv_label_set_text_fmt(label_acc, "#ff0000 X:# %d\t#00ff00 Y:# %d\t#0000ff Z:# %d", x, y, z);
          lv_label_set_text_fmt(label_step, "%d", stepcount);
        }
      }
    }
  }

  if (require == 1)
  {
    require = 0;
    acc_data_send();
  }

}

/*解析JSON函数*/
void json_to_str(const char *json_str)
{
  cJSON *pJsonRoot = cJSON_Parse(json_str);
  if (pJsonRoot != NULL)
  {
    cJSON *pRequire = cJSON_GetObjectItem(pJsonRoot, "require");
    if (!pRequire)
    {
      Serial.printf("no acc require\n");
    }
    else
    {
      if (cJSON_IsNumber(pRequire))
      {
        require = pRequire->valueint;
        return;
      }
    }

    cJSON *pWatch_data = cJSON_GetObjectItem(pJsonRoot, "watch_data"); /*解析手表字符串信息*/
    if (!pWatch_data)
    {
      Serial.printf("pwatch_data error");
      return;
    }
    else
    {
      if (cJSON_IsString(pWatch_data))
      {
        memset(watch_data, 0, sizeof(watch_data));
        strcpy(watch_data, pWatch_data->valuestring);
        if (CONCTRL_STATE)
        {
          lv_label_set_text(user_label, watch_data);
        }
      }
    }

    cJSON *pTem = cJSON_GetObjectItem(pJsonRoot, "temperature"); /*解析温度数据*/
    if (!pTem)
      return;
    else
    {
      if (cJSON_IsNumber(pTem))
      {
        temperature = pTem->valueint;
      }
    }

    cJSON *pHum = cJSON_GetObjectItem(pJsonRoot, "humidity"); /*解析湿度数据*/
    if (!pHum)
      return;
    else
    {
      if (cJSON_IsNumber(pHum))
      {
        humidity = pHum->valueint;
      }
    }

    cJSON *pPM2 = cJSON_GetObjectItem(pJsonRoot, "PM2_5"); /*解析PM2.5数据*/
    if (!pPM2)
      return;
    else
    {
      if (cJSON_IsNumber(pPM2))
      {
        PM2_5 = pPM2->valueint;
      }
    }

    cJSON *pLight = cJSON_GetObjectItem(pJsonRoot, "light"); /*解析灯的状态*/
    if (!pLight)
      return;
    else
    {
      if (cJSON_IsNumber(pLight))
      {
        if (SW1_STATE != pLight->valueint)
        {
          SW1_STATE = pLight->valueint;
          if (CONCTRL_STATE)
          {
            lv_switch_toggle(sw1, LV_ANIM_ON);
            light_img_show();
          }
        }
      }
    }

    cJSON *pAir = cJSON_GetObjectItem(pJsonRoot, "air"); /*解析灯的状态*/
    if (!pAir)
      return;
    else
    {
      if (cJSON_IsNumber(pAir))
      {
        if (SW2_STATE != pAir->valueint)
        {
          SW2_STATE = pAir->valueint;
          if (CONCTRL_STATE)
          {
            lv_switch_toggle(sw2, LV_ANIM_ON);
            air_img_show();
          }
        }
      }
    }

    cJSON *pCurtains = cJSON_GetObjectItem(pJsonRoot, "curtains");
    if (!pCurtains)
      return;
    else
    {
      cJSON *pCur_state = cJSON_GetObjectItem(pCurtains, "state");
      if (!pCur_state)
        return;
      else
      {
        if (cJSON_IsNumber(pCur_state))
        {
          if (SW3_STATE != pCur_state->valueint)
          {
            SW3_STATE = pCur_state->valueint;
            if (CONCTRL_STATE)
            {
              curtains_img_show();
            }
          }
        }
      }
      cJSON *pCur_open = cJSON_GetObjectItem(pCurtains, "open_degree");
      if (!pCur_open)
        return;
      else
      {
        if (cJSON_IsNumber(pCur_open))
        {
          curtains_open = pCur_open->valueint;
          if (CONCTRL_STATE)
          {
            lv_slider_set_value(cur_slider, curtains_open, LV_ANIM_ON);
          }
        }
      }
    }

    cJSON *pToday = cJSON_GetObjectItem(pJsonRoot, "today");
    if (!pToday)
      return;
    else
    {
      cJSON *Today_year = cJSON_GetObjectItem(pToday, "year");
      if (!Today_year)
        return;
      else
      {
        if (cJSON_IsNumber(Today_year))
        {
          today.year = Today_year->valueint;
        }
      }
      cJSON *Today_month = cJSON_GetObjectItem(pToday, "month");
      if (!Today_month)
        return;
      else
      {
        if (cJSON_IsNumber(Today_month))
        {
          today.month = Today_month->valueint;
        }
      }
      cJSON *Today_day = cJSON_GetObjectItem(pToday, "day");
      if (!Today_day)
        return;
      else
      {
        if (cJSON_IsNumber(Today_day))
        {
          today.day = Today_day->valueint;
        }
      }
    }

    cJSON *Bright = cJSON_GetObjectItem(pJsonRoot, "bright_data");
    if (!Bright)
      return;
    else
    {
      int arrlen = cJSON_GetArraySize(Bright);
      int i;
      for (i = 0; i < arrlen; i++)
      {
        bright_data[i] = cJSON_GetArrayItem(Bright, i)->valueint;
      }
    }

    cJSON *pTime = cJSON_GetObjectItem(pJsonRoot, "time");
    if (!pTime)
      return;
    else
    {
      cJSON *pTime_hour = cJSON_GetObjectItem(pTime, "hour");
      if (!pTime_hour)
        return;
      else
      {
        if (cJSON_IsNumber(pTime_hour))
        {
          now_time.hour = pTime_hour->valueint;
        }
      }
      cJSON *pTime_min = cJSON_GetObjectItem(pTime, "min");
      if (!pTime_min)
        return;
      else
      {
        if (cJSON_IsNumber(pTime_min))
        {
          now_time.min = pTime_min->valueint;
        }
      }
      cJSON *pTime_sec = cJSON_GetObjectItem(pTime, "sec");
      if (!pTime_sec)
        return;
      else
      {
        if (cJSON_IsNumber(pTime_sec))
        {
          now_time.sec = pTime_sec->valueint;
        }
      }
    }
  }
  else
  {
    Serial.printf("DATA WAS NOT A JSON");
  }
}

/*封装JSON函数*/
void send_json()
{
  cJSON *pRoot = cJSON_CreateObject();
  cJSON_AddNumberToObject(pRoot, "light", SW1_STATE);
  cJSON_AddNumberToObject(pRoot, "air", SW2_STATE);

  cJSON *pCur = cJSON_CreateObject();
  cJSON_AddItemToObject(pRoot, "curtains", pCur);
  cJSON_AddNumberToObject(pCur, "open_degree", curtains_open);
  cJSON_AddNumberToObject(pCur, "state", SW3_STATE);

  char *sendData = cJSON_Print(pRoot);
  Serial.printf("len: %d ", strlen(sendData));
  SerialBT.write((const uint8_t *)sendData, strlen(sendData));
}

void watch_time_count()
{
  no_touch_time += 1;
  if (no_touch_time > 120 && WATCH_SHOWING == false) //更改多少秒后休眠
  {
    WATCH_SHOWING = true;
    no_touch_time = 0;
    analog();
    lv_scr_load_anim(central, LV_SCR_LOAD_ANIM_FADE_ON, 200, 0, false);
  }
}

void acc_data_send()
{
  if (SerialBT.connected())
  {
    cJSON *pRoot = cJSON_CreateObject();
    cJSON *pAcc = cJSON_CreateObject();
    cJSON_AddItemToObject(pRoot, "position", pAcc);
    cJSON_AddNumberToObject(pAcc, "x", x);
    cJSON_AddNumberToObject(pAcc, "y", y);
    cJSON_AddNumberToObject(pAcc, "z", z);
    cJSON_AddNumberToObject(pAcc, "stepcount", stepcount);
    Serial.printf("X:%d  Y:%d  Z:%d\n", x, y, z);

    char *sendData = cJSON_Print(pRoot);
    Serial.printf("len: %d ", strlen(sendData));
    SerialBT.write((const uint8_t *)sendData, strlen(sendData));
  }
}

/*-------------------------------------------待机表盘-----------------------------------------------*/

void update_time(lv_task_t *task)
{

  if (lvHour != NULL)
  {
    uint16_t h = now_time.hour * 300 + now_time.min / 12 % 12 * 60;
    lv_img_set_angle(lvHour, h);
    lv_img_set_angle(lvMinute, now_time.min * 6 * 10);

    lv_img_set_angle(lvSecond, now_time.sec * 6 * 10);
    //printf("h %d m %d s %d\n", Hour, Minute, Second);
  }

  if (++now_time.sec >= 60)
  {
    now_time.sec = 0;
    if (++now_time.min >= 60)
    {
      now_time.min = 0;
      if (++now_time.hour >= 12)
        now_time.hour = 0;
    }
  }
}

void analog()
{
  if (WATCH_STATE == false)
  {
    WATCH_STATE = true;

    central = lv_obj_create(NULL, NULL);

    lv_obj_set_size(central, 240, 240);
    lv_obj_t *img1 = lv_img_create(central, NULL);
    lv_img_set_src(img1, &watch_bg);

    lv_obj_align(img1, central, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_size(img1, 240, 240);

    lvHour = lv_img_create(central, NULL);
    lv_img_set_src(lvHour, &hour);
    lv_obj_align(lvHour, img1, LV_ALIGN_CENTER, 0, 0);
    uint16_t h = now_time.hour * 300 + now_time.min / 12 % 12 * 60;
    lv_img_set_angle(lvHour, h);

    lvMinute = lv_img_create(central, NULL);
    lv_img_set_src(lvMinute, &minute);
    lv_obj_align(lvMinute, img1, LV_ALIGN_CENTER, 0, 0);
    lv_img_set_angle(lvHour, now_time.min * 60);

    lvSecond = lv_img_create(central, NULL);
    lv_img_set_src(lvSecond, &second);
    lv_obj_align(lvSecond, img1, LV_ALIGN_CENTER, 0, 0);
    lv_img_set_angle(lvSecond, now_time.sec * 60);

    lv_obj_set_event_cb(central, into_exit_event_handler); //时钟页面被点击后进入主界面

    lv_task_create(update_time, 1000, LV_TASK_PRIO_LOW, NULL);
  }
}

static uint8_t conv2d(const char *p)
{
  uint8_t v = 0;
  if ('0' <= *p && *p <= '9')
    v = *p - '0';
  return 10 * v + *++p - '0';
}

/*多任务任务测试函数*/
void detect_tft_is_been_touch_TASK(void *par)
{
  while (1)
  {
    delay(20);
  }
}

